package com.example.com.test.util;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 作者： ch
 * 时间： 2018/6/8 0008-下午 4:20
 * 描述：
 * 来源：
 */


public class DateUtils {

    public static String getTime(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(new Date(time));
    }

    public static String getDate(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date(time));
    }

    public static String getHours(long time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(new Date(time));
    }


    public static long getDate(String time) {
        if (time.contains("-") && time.contains(":")) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                return format.parse(time).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
                return 0;
            }
        }
        return 0;
    }


    public static double getDatesByToday(String time) {

        long today = new Date().getTime();
        long dates = getDate(time);
        return (double) (today - dates) / 3600 / 24 / 1000;

    }

    /**
     * 把毫秒值 转出**:**
     *
     * @param time
     * @return
     */
    public static String getTimeByLong(String time) {

        if (TextUtils.isEmpty(time)) {
            return "0";
        }
        return getTimeByLong(ParseUtils.parseLong(time));
    }

    /**
     * 把毫秒值 转出**:**
     *
     * @param time
     * @return
     */
    public static String getTimeByLong(long time) {
        int t = (int) (time / 1000);

        if (t < 0) {
            t = t * -1;
        }
        if (t > 60) {
            int minute = t / 60;
            int secend = t % 60;

            if (secend >= 0 && secend < 10) {
                return minute + ":0" + t % 60;
            } else {
                return minute + ":" + t % 60;
            }

        } else {
            int secend = t % 60;
            if (secend > 0 && secend < 10) {
                return "0:0" + t % 60;
            } else {
                return "0:" + t % 60;
            }

        }
    }

}
